# -*- coding: utf-8 -*-

#    OriginDB
#    Copyright (C) 2009  Jason Power <power.jg@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from PyQt4 import QtCore, QtGui
from ui_visualWindow import Ui_VisualWindow

from sys import path
path.append("../")

import database

from compoundVisual import CompoundVisual
from reactionVisual import ReactionVisual
from objectVisual import ObjectVisual
from connection import Connection

class VisualWindow(QtGui.QMainWindow):
	def __init__(self, parent, controller):
		QtGui.QMainWindow.__init__(self, parent)
		
		self.controller = controller
		
		# Set up the user interface from Designer.
		self.ui = Ui_VisualWindow()
		self.ui.setupUi(self)
		
		self.baseCompounds = []
		
		self.scene = QtGui.QGraphicsScene()
		self.ui.mainGraphicsView.setScene(self.scene)
		
		self.adenine = database.Compound(3)
		self.c1 = self.addCompound(self.adenine)
		
		self.baseCompounds.append(self.c1)
		
		self.addProducts(self.c1)
		
		self.wpad = 75
		self.hpad = 10
		
		for compound in self.c1.products:
			self.addProducts(compound)
			
		self.ui.searchResultsFrame = self.controller.searchResultsFrame
			 
		self.connect(self.ui.cleanPushButton, QtCore.SIGNAL("clicked()"), self.cleanDisplay)
		self.connect(self.ui.searchPushButton, QtCore.SIGNAL("clicked()"), self.searchCompound)
		
		self.cleanDisplay(self.wpad, self.hpad)
	
	def addObject(self, obj):
		self.scene.addItem(obj)
		
	def addCompound(self,compound):
		vis = CompoundVisual(compound)
		self.addObject(vis)
		return vis
		
	def addConnection(self, obj1, obj2):
		connection = Connection(obj1, obj2)
		self.scene.addItem(connection)
		return connection
		
	def addProducts(self, cVis):
		relations = database.searchForProducts(cVis.compound)
		for c, r in relations:
			a = self.addCompound(c)
			cVis.addProduct(a)
			self.addConnection(cVis, a)
			
	def cleanDisplay(self, wpad=-1, hpad=-1):
		if wpad < 0:
			wpad = self.wpad
		if hpad < 0:
			hpad = self.hpad
		
		if not self.baseCompounds:
			return
		for item in self.baseCompounds:
			item.cleanChildren(wpad, hpad)
		
		item = self.baseCompounds[0]
		colliding = [i for i in item.collidingItems() if isinstance(i, ObjectVisual)]
		rect = item.boundingRect() | item.childrenBoundingRect()
		totalHeight = 0
		for jtem in colliding:
			rect = jtem.boundingRect() | jtem.childrenBoundingRect()
			totalHeight += rect.height() + hpad
		for jtem in colliding:
			jtem.moveBy(0, -totalHeight)
			rect = jtem.boundingRect() | jtem.childrenBoundingRect()
			totalHeight -= rect.height() - hpad
		
	def searchCompound(self):
		self.controller.searchCompoundDialog("Visual", self)
		self.ui.searchResultsFrame = self.controller.searchResultsFrame
		self.ui.searchResultsFrame.show()